<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class DateController extends Controller{

    // 日期显示
    public function date_table(Request $request){
        $page=$request->get('page');
        $limit=$request->get('limit');
        $crows=DB::table('index_date')->get();
        $rows=Db::table('index_date')->simplePaginate($limit,'*',$limit,$page);
        $data=[
            "code"=>0,
            "msg"=>'',
            "count"=>count($crows),
            "data"=>$rows->items(),
            "limit"=>$limit
        ];
        return json_encode($data);
    }
    //时间
    public function date_add(){
        $arr=[
            '8:00','8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30',
            '14:00','14:30','15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30',
            '20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30','24:00'
            ];
        $rows=DB::table('index_aunt')->get();
        return view('admin.date.date_add',["rows"=>$arr,'ay'=>$rows]);
    }
    //添加时间
    public function date_insert(Request $request){
        $data=$request->post('result');
        $arr2['date']=$data['date'];
        $aytime['pid']=$data['city'];
        unset($data["date"]);
        unset($data['city']);
        foreach ($data as $k=>$v){
            $arr[]=$v;
            $arr3[]='0';
        };
        $atime=json_encode($arr3);
        $time=json_encode($arr);
        $arr2['time']=$time;
        $aytime['aytime']=$atime;
        $rows=DB::table('index_date')->insert($arr2);
        $rid=DB::table('index_date')->orderBy("id",'desc')->value('id');
        $aytime['rid']=$rid+1;
        $rows2=DB::table('index_time')->insert($aytime);
        return 1;
    }
    //修改回显
    public function date_edit($id){
        $rows=DB::table('index_date')->where('id',$id)->first();
        $arr=json_decode($rows->time);
        $ay=DB::table('index_aunt')->get();
        $rows2=DB::table('index_time')->where('rid',$id)->value('pid');
        return view('admin.date.date_edit',["rows"=>$rows,'sj'=>$arr,'ay'=>$ay,'pid'=>$rows2]);
    }
    //修改
    public function date_update(Request $request){
        $data=$request->post('result');
        $arr3['pid']=$data['city'];
        $arr3['rid']=$data['id'];
        $arr2['id']=$data['id'];
        $arr2['date']=$data['date'];
        unset($data["id"]);
        unset($data["date"]);
        unset($data['city']);
        foreach ($data as $k=>$v){
            $arr[]=$v;
            $arr4[]='0';
        };
        $time=json_encode($arr);
        $arr2['time']=$time;
        $arr3['aytime']=json_encode($arr4);
        $rows=DB::table('index_date')->where('id',$arr2['id'])->update($arr2);
        $rows2=DB::table('index_time')->insert($arr3);
            return 1;
    }
    //删除
    public function date_del(Request $request){
        $id=$request->get('id');
        $rows=DB::table('index_date')->where('id',$id)->delete();
        $rows2=DB::table('index_time')->where('rid',$id)->delete();
        return 1;
    }
}